package com.wxx;

/**
 * @author: Jetwang
 * @date: 2022/4/1 21:41
 * @description: LinkedList 链表
 * @version: 1.0
 */
public class LinkedList<T> {
    /**
     * 头节点
     */
    private Node<T> dummyHead;
    /**
     * 链表大小
     */
    private int size;

    public LinkedList() {
        dummyHead = new Node(null, null);
        size = 0;
    }

    /**
     * 返回链表元素个数
     * @return
     */
    public int getSize() {
        return size;
    }

    /**
     * 判断链表是否为空
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 头部添加元素
     * @param t
     */
    public void addFirst(T t) {
        /*Node<T> node = new Node<>(t);
        node.next = head;
        head = node;*/

        // 一行代码
        /*head = new Node(t, head);
        size++;*/

        add(0, t);
    }

    /**
     * 在索引为index的位置插入节点
     * @param index
     * @param t
     */
    public void add(int index, T t) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed, illegal index");
        }

        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            // prev一直后移
            prev = prev.next;
        }
        // 添加元素进去
        /*Node<T> node = new Node<>(t);
        node.next = prev.next;
        prev.next = node;*/

        // 一行代码
        prev.next = new Node(t, prev.next);
        size++;
    }

    /**
     * 在链表末尾添加元素
     * @param t
     */
    public void addLast(T t) {
        add(size, t);
    }

    /**
     * 获取第index位置的元素
     * @param index
     * @return
     */
    public T get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("get failed, illegal index");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return (T) cur.t;
    }

    /**
     * 获取第一个元素
     * @return
     */
    public T getFirst() {
        return get(0);
    }

    /**
     * 获取最后第一个元素
     * @return
     */
    public T getLast() {
        return get(size - 1);
    }

    /**
     * 更新节点
     * @param t
     */
    public void set(int index, T t) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("set failed, illegal index");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.t = t;
    }

    /**
     * 删除节点
     * @param index
     * @return
     */
    public T remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("remove failed, illegal index");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;
        return (T) retNode.t;
    }

    /**
     * 链表中是否包含某元素
     * @param t
     * @return
     */
    public boolean contains(T t) {
        Node cur = dummyHead.next;
        while (cur != null) {
            if (cur.t.equals(t)) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null) {
            res.append(cur.t + "->");
            cur = cur.next;
        }
        res.append("null");
        return res.toString();
    }
}
